package cn.lovelycatv.custom.listener;

import cn.lovelycatv.custom.CustomItemObject;
import cn.lovelycatv.custom.VaultAPI;
import cn.lovelycatv.custom.SoltObject;
import cn.marinda.spritefragment.SpriteFragment;
import cn.marinda.spritefragment.data.ConfigManager;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;

import java.util.List;
import java.util.Map;

public class ContainerEvent implements Listener {
    private ConfigManager gm = null;
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        gm = new ConfigManager();
        HumanEntity humanEntity = event.getWhoClicked();
        InventoryView inventoryView = humanEntity.getOpenInventory();
        String title = inventoryView.getTitle();
        int clicked = event.getRawSlot();
        //System.out.println(clicked);
        if (title.equalsIgnoreCase(gm.getConfigData().getInvName())) {
            // 点击工作区与按钮以外直接取消
            boolean isCancelClick = true;
            for (int i : cn.lovelycatv.custom.GUI.SynthesisGUI.getWorkPlace()) {
                if (clicked == i) {
                    isCancelClick = false;
                }
            }
            if (clicked >= 54 && clicked <= 89) {
                isCancelClick = false;
            }
            if (clicked == 35) {
                isCancelClick = false;
            }
            event.setCancelled(isCancelClick);
            // 这里是合成
            ItemStack result = null;
            ItemStack top1;
            ItemStack left2;
            ItemStack center3;
            ItemStack bottom4;
            ItemStack right5;
            top1 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getTop());
            left2 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getLeft());
            center3 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getCenter());
            bottom4 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getBottom());
            right5 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getRight());
            ItemStack[] items = {top1,left2,center3,bottom4,right5};
            if (clicked == cn.lovelycatv.custom.GUI.SynthesisGUI.getConfirm()) {
                // 检查exchange配方表
                Map<String, CustomItemObject> exchangeMap = gm.getExchangeConfig().getMap();
                for (Map.Entry<String, CustomItemObject> entry : exchangeMap.entrySet()) {
                    boolean isPass = false;
                    String parentName = entry.getKey();
                    CustomItemObject customItemObject = entry.getValue();
                    Map<Integer, SoltObject> solts = customItemObject.getSolts();
                    int i = 1;
                    for (ItemStack soltItem : items) {
                        SoltObject soltObject = solts.get(i);
                        String mark = soltObject.getMark();
                        int num = soltObject.getNum();
                        // 先检查物品数量
                        if (soltItem.getAmount() == num) {
                            // 通过检查 继续检查Lore与DisplayName
                            ItemStack markItem = gm.getItemData().getItems(mark);
                            if (soltItem.getItemMeta().getDisplayName().equalsIgnoreCase(markItem.getItemMeta().getDisplayName())) {
                                // DisplayName相同 继续判断Lore
                                List<String> markItemLore = markItem.getItemMeta().getLore();
                                List<String> soltItemLore = soltItem.getItemMeta().getLore();
                                if (markItemLore.equals(soltItemLore)) {
                                    // 检查通过
                                    isPass = true;
                                }else {
                                    // 检查不通过
                                    isPass = false;
                                    break;
                                }
                            }
                            isPass = true;
                        }else {
                            // 检查不通过
                            isPass = false;
                            break;
                        }
                        i++;
                    }
                    if (isPass) {
                        // 通过配方检查 放出合成物
                        boolean canGiveItem = false;
                        Player player = Bukkit.getServer().getPlayer(humanEntity.getName());
                        if (gm.getConfigData().isVaule()) {
                            // 扣钱！！！
                            try {
                                VaultAPI vaultAPI = new VaultAPI(SpriteFragment.getEco());
                                double playerMoney = vaultAPI.getMoney(player);
                                double shouldPay = gm.getExchangeConfig().getItemPayNumber(parentName);
                                if (playerMoney >= shouldPay) {
                                    vaultAPI.takeMoney(player, shouldPay);
                                    canGiveItem = true;
                                } else {
                                    player.sendMessage("§c你没有足够的钱合成！");
                                    canGiveItem = false;
                                }
                            }catch (NullPointerException exception) {
                                System.out.println("你没有安装Vault但是你打开了合成付费！");
                            }catch (NoClassDefFoundError exception) {
                                System.out.println("你没有安装Vault但是你打开了合成付费！");
                            }
                        }else {
                            // 跳过Vault
                            canGiveItem = true;
                        }

                        if (canGiveItem) {
                            result = gm.getItemData().getResultItems(gm.getConfigData().getExchangeResule(parentName));
                            inventoryView.setItem(35,result);
                            for (int a : cn.lovelycatv.custom.GUI.SynthesisGUI.getWorkPlace()) {
                                inventoryView.setItem(a,null);
                            }
                        }
                    }
                }
            }else if (clicked == cn.lovelycatv.custom.GUI.SynthesisGUI.getDismiss()) {
                //System.out.println("MDDDDDD");
                humanEntity.closeInventory();
            }
        }
    }

    @EventHandler
    public void onCloseInventory(InventoryCloseEvent event) {
        gm = new ConfigManager();
        HumanEntity humanEntity = event.getPlayer();
        Inventory inventoryView = event.getInventory();
        if (inventoryView.getTitle() == null) {
            return;
        }
        if (inventoryView.getTitle().equalsIgnoreCase(gm.getConfigData().getInvName())) {
            // 首先还物品
            ItemStack result = null;
            ItemStack top1;
            ItemStack left2;
            ItemStack center3;
            ItemStack bottom4;
            ItemStack right5;
            top1 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getTop());
            left2 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getLeft());
            center3 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getCenter());
            bottom4 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getBottom());
            right5 = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getRight());
            ItemStack[] items = {top1,left2,center3,bottom4,right5};

            result = inventoryView.getItem(cn.lovelycatv.custom.GUI.SynthesisGUI.getResult());
            for (ItemStack itemStack : items) {
                if (itemStack != null) {
                    humanEntity.getInventory().addItem(itemStack);
                }
            }
            if (result != null) {
                humanEntity.getInventory().addItem(result);
            }
        }
    }
}
